﻿using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Collections.Generic;
using ZhongchenZhou.EnterpriseLibrary.Logging.DynamicsCrm2011.Configuration.Fluent;

namespace ZhongchenZhou.EnterpriseLibrary.Logging.DynamicsCrm2011.SampleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string, object> logExtraInfo = new Dictionary<string, object>();
            logExtraInfo.Add("DiscoveryServiceUri", "");
            logExtraInfo.Add("OrgUniqueName", "zhongchenzhou");
            logExtraInfo.Add("EntityName", "zzhou_entliblog");

            LogEntry entry = new LogEntry();
            entry.Message = "This is a test log message";
            entry.Categories.Add("Test Category");
            entry.EventId = 1024;
            entry.Severity = System.Diagnostics.TraceEventType.Error;
            entry.ExtendedProperties = logExtraInfo;

            //LogWithConfigFile(entry);
            LogWithFluentInterface(entry);
        }

        private static void LogWithConfigFile(LogEntry entry)
        {
            var logWritter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

            logWritter.Write(entry);
        }

        private static void LogWithFluentInterface(LogEntry entry)
        {
            var builder = new ConfigurationSourceBuilder();

            builder.ConfigureLogging()
                   .WithOptions
                     .DoNotRevertImpersonation()
                   .SpecialSources.AllEventsCategory
                     .SendTo.DynamicsCrm2011("Microsoft Dynamics CRM 2011 Trace Listener").UseCrmConnection("Crm")
                        .FormatWith(new FormatterBuilder()
                             .TextFormatterNamed("Text Formatter")
                               .UsingTemplate(@"Timestamp: {timestamp}{newline}
                                                Message: {message}{newline}
                                                Category: {category}{newline}
                                                Priority: {priority}{newline}
                                                EventId: {eventid}{newline}
                                                Severity: {severity}{newline}
                                                Title:{title}{newline}
                                                Machine: {localMachine}{newline}
                                                App Domain: {localAppDomain}{newline}
                                                ProcessId: {localProcessId}{newline}
                                                Process Name: {localProcessName}{newline}
                                                Thread Name: {threadName}{newline}
                                                Win32 ThreadId:{win32ThreadId}{newline}
                                                Extended Properties: {dictionary({key} - {value}{newline})}"));

            var configSource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(configSource);
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

            var logWritter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

            logWritter.Write(entry);
        }
    }
}
